package com.hust360.percent.pojo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.hust360.percent.PercentApplication;
import com.hust360.percent.pojo.Dsc_usr.Dsc_usrDAO;

public class Dsc_usrDAOImpl implements Dsc_usrDAO {

	@Override
	public Dsc_usr findById(Long id) {
		Dsc_usr dsc_usr = null;
		Cursor c = PercentApplication.localDB.rawQuery(
				"SELECT * FROM dsc_usr WHERE id='" + id + "'", null);
		if (c.moveToFirst()) {
			dsc_usr = new Dsc_usr();
			dsc_usr.id = c.getLong(c.getColumnIndex(Dsc_usr.ID));
			dsc_usr.dscid = c.getLong(c.getColumnIndex(Dsc_usr.DSCID));
			dsc_usr.uid = c.getLong(c.getColumnIndex(Dsc_usr.UID));
		}
		c.close();
		return dsc_usr;
	}

	@Override
	public Dsc_usr findByUsrId(Long uid) {
		Dsc_usr dsc_usr = null;
		Cursor c = PercentApplication.localDB.rawQuery(
				"SELECT * FROM dsc_usr WHERE uid='" + uid + "'", null);
		if (c.moveToFirst()) {
			dsc_usr = new Dsc_usr();
			dsc_usr.id = c.getLong(c.getColumnIndex(Dsc_usr.ID));
			dsc_usr.dscid = c.getLong(c.getColumnIndex(Dsc_usr.DSCID));
			dsc_usr.uid = c.getLong(c.getColumnIndex(Dsc_usr.UID));
		}
		c.close();
		return dsc_usr;
	}

	@Override
	public Dsc_usr findByDscid(Long dscid) {
		Dsc_usr dsc_usr = null;
		Cursor c = PercentApplication.localDB.rawQuery(
				"SELECT * FROM dsc_usr WHERE dscid='" + dscid + "'", null);
		if (c.moveToFirst()) {
			dsc_usr = new Dsc_usr();
			dsc_usr.id = c.getLong(c.getColumnIndex(Dsc_usr.ID));
			dsc_usr.dscid = c.getLong(c.getColumnIndex(Dsc_usr.DSCID));
			dsc_usr.uid = c.getLong(c.getColumnIndex(Dsc_usr.UID));
		}
		c.close();
		return dsc_usr;
	}

	@Override
	public void save(Dsc_usr dsc_usr) {
		ContentValues values = new ContentValues();
		values.put(Dsc_usr.ID, dsc_usr.id);
		values.put(Dsc_usr.UID, dsc_usr.uid);
		values.put(Dsc_usr.DSCID, dsc_usr.dscid);
		synchronized (PercentApplication.localDB) {
			PercentApplication.localDB.insertWithOnConflict(Dsc_usr.TABLE,
					null, values, SQLiteDatabase.CONFLICT_REPLACE);
		}
	}

	@Override
	public List<Dsc_usr> findWithMap(Map<String, String> map) {
		StringBuffer selection = new StringBuffer();
		String value = null;
		List<Dsc_usr> list = null;
		Dsc_usr dsc_usr = null;
		List<String> columns = Arrays.asList(Dsc_usr.COLUMNS);
		Set<String> keySet = map.keySet();
		for (String key : keySet) {
			value = map.get(key);
			if (columns.contains(key) && value != null) {
				selection.append(key + "='" + value + "'");
				value = null;
			}
		}
		selection.append("'1'='1'");

		Cursor c = PercentApplication.localDB.rawQuery(
				"SELECT * FROM dsc_usr WHERE " + selection.toString(), null);
		list = new ArrayList<Dsc_usr>(c.getCount());
		while (c.moveToNext()) {
			dsc_usr = new Dsc_usr();
			dsc_usr.id = c.getLong(c.getColumnIndex(Dsc_usr.ID));
			dsc_usr.dscid = c.getLong(c.getColumnIndex(Dsc_usr.DSCID));
			dsc_usr.uid = c.getLong(c.getColumnIndex(Dsc_usr.UID));
			list.add(dsc_usr);
		}
		c.close();
		return list;
	}

	@Override
	public boolean hasRelation(Map<String, String> map) {
		List<String> keyArray = Arrays.asList(Dsc_usr.COLUMNS);
		Set<String> keySet = map.keySet();
		String value = null;
		StringBuffer selection = new StringBuffer();
		for (String key : keySet) {
			value = map.get(key);
			if (keyArray.contains(key) && value != null) {
				selection.append(key + "='" + value + "' AND ");
				value = null;
			}
		}
		selection.append("'1'='1'");
		Cursor c = PercentApplication.localDB.rawQuery(
				"SELECT * FROM dsc_usr WHERE " + selection.toString(), null);
		int count = c.getCount();
		c.close();
		return count > 0 ? true : false;
	}

}
